package com.examsage.examsage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.examsage.examsage.model.Course;
import com.examsage.examsage.mapper.CourseMapper;
import com.examsage.examsage.model.vo.ResponseVO;
import com.examsage.examsage.service.ICourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author examsage
 * @since 2025-07-02
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements ICourseService {

    @Resource
    private CourseMapper courseMapper;

    @Override
    public ResponseVO addCourse(Course course){
        course.setGmtCreate(LocalDateTime.now());
        course.setGmtModified(LocalDateTime.now());
        ResponseVO responseVO = new ResponseVO();
        if(courseMapper.insert(course) > 0){
            responseVO.setStatusCode(200);
            responseVO.setMessage("添加成功");
            responseVO.setData(course);
        } else {
            responseVO.setStatusCode(500);
            responseVO.setMessage("添加失败");
            responseVO.setData(null);
        }
        return responseVO;
    }

    @Override
    public ResponseVO getCourseList(int page, int size, String courseName, String courseCode){
        //  TODO 查询课程列表（支持分页和条件查询）
        // 构建查询条件
        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();

        // 按专业名称模糊查询
        if (courseName != null && !courseName.trim().isEmpty()) {
            queryWrapper.like("course_name", courseName);
        }

        // 按状态筛选（1:启用，0:停用）
        if (courseCode != null && !courseCode.trim().isEmpty()) {
            queryWrapper.eq("course_code", courseCode);
        }

        // 分页查询
        com.baomidou.mybatisplus.extension.plugins.pagination.Page<Course> pageQuery
                = new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(page, size);

        ResponseVO responseVO = new ResponseVO();

        try {
            // 执行分页查询
            com.baomidou.mybatisplus.extension.plugins.pagination.Page<Course> resultPage
                    = courseMapper.selectPage(pageQuery, queryWrapper);

            responseVO.setStatusCode(200);
            responseVO.setMessage("查询专业计划成功");
            responseVO.setData(resultPage);
        } catch (Exception e) {
            responseVO.setStatusCode(500);
            responseVO.setMessage("查询专业计划失败");
            responseVO.setData(null);
        }

        return responseVO;
    }


    @Override
    public ResponseVO getCourseById(Long id){
        ResponseVO responseVO = new ResponseVO();
        Course course = courseMapper.selectById(id);
        if(course != null){
            responseVO.setStatusCode(200);
            responseVO.setMessage("查询成功");
            responseVO.setData(course);
        } else {
            responseVO.setStatusCode(500);
            responseVO.setMessage("查询失败");
            responseVO.setData(null);
        }
        return responseVO;
    }

    @Override
    public ResponseVO updateCourse(Long id, Course course){
        UpdateWrapper<Course> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id);

        if (course.getCourseName() != null && !course.getCourseName().trim().isEmpty()) {
            updateWrapper.set("course_name", course.getCourseName());
        }
        if (course.getCourseCode() != null && !course.getCourseCode().trim().isEmpty()) {
            updateWrapper.set("course_code", course.getCourseCode());
        }
        if (course.getCourseType() != null && !course.getCourseType().trim().isEmpty()) {
            updateWrapper.set("course_type", course.getCourseType());
        }
        if (course.getCredits() != null) {
            updateWrapper.set("credits", course.getCredits());
        }

        // 自动填充 gmt_modified（如果已配置 MetaObjectHandler）
        ResponseVO responseVO = new ResponseVO();
        if (courseMapper.update(null, updateWrapper) > 0) {
            responseVO.setStatusCode(200);
            responseVO.setMessage("修改专业计划成功");
            responseVO.setData(courseMapper.selectById(id));
        } else {
            responseVO.setStatusCode(500);
            responseVO.setMessage("修改专业计划失败");
            responseVO.setData(null);
        }
        return responseVO;
    }

    @Override
    public ResponseVO deleteCourse(Long id){
        ResponseVO responseVO = new ResponseVO();
        if(courseMapper.deleteById(id) > 0){
            responseVO.setStatusCode(200);
            responseVO.setMessage("删除成功");
            responseVO.setData(null);
        } else {
            responseVO.setStatusCode(500);
            responseVO.setMessage("删除失败");
            responseVO.setData(null);
        }
        return responseVO;
    }

}
